<?php
	// load the keys from the database based on the "gainID" from the link.
	$gainid = $_GET["gainid"];
	$db = mysql_connect("mysql513.in.shared-server.net:16033", "JRtu434", "qvoiceDB1");
	mysql_select_db("JRtu434", $db);
	$result = mysql_query("Select key1,key2,url from ghostprojects where id = \"".$gainid."\"");
	$record = mysql_fetch_array($result);
	$key1 = $record[0];
	$key2 = $record[1];
	$url = $record[2];
	
    foreach($_GET as $key => $value) {
		if($key == $key1) {$key1value = $value;}
		elseif($key == $key2) {$key2value = $value;}
	}
	// check if this project has been started
	$result = mysql_query("select count(id) from ghostmonitors where gainid=\"$gainid\" and key1value=\"$key1value\" and key2value=\"$key2value\"");
	$check = mysql_fetch_array($result);
	
	if($check[0]==0){
		mysql_query("insert into ghostmonitors (gainid,key1value,key2value,status,starttime) values (\"$gainid\",\"$key1value\",\"$key2value\",\"started\",\"".time()."\")");
		//insert the values into the table and set the status to started
		$result = mysql_query("select id from ghostmonitors where gainid=\"$gainid\" and key1value=\"$key1value\" and key2value=\"$key2value\"");
		$res = mysql_fetch_array($result);
		$id = $res[0];
	}
	else {
		// set the status to "retried"
		$result = mysql_query("select id from ghostmonitors where gainid=\"$gainid\" and key1value=\"$key1value\" and key2value=\"$key2value\"");
		$res = mysql_fetch_array($result);
		$id = $res[0];
		mysql_query("Update ghostmonitors set status=\"retried\" where id=$id");
	}
	$nurl = $url.$id;
mysql_close();
header("Location: " . $nurl);
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="ja">
<!-- ==== document header ==== -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title></title>
</head>
</html>